package com.hopes.bizz.protocol.hj212.activemq.consumer;

import com.hopes.bizz.protocol.hj212.activemq.config.ActiveMqStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/5/25 13:57
 * @Description:
 */
@Component
@Slf4j
public class ActiveMqQueueConsumer {
    @Autowired
    private ActiveMqStore activeMqStore;

    public MessageConsumer getConsumer(String queue, MessageListener messageListener) {
        MessageConsumer consumer = null;
        try {
            Session session = activeMqStore.getSession();
            consumer = session.createConsumer(session.createQueue(queue));
            consumer.setMessageListener(messageListener);
        } catch (JMSException e) {
            log.error("消息消费失败,错误码={},原因={}", e.getErrorCode(), e.getMessage());
        }
        return consumer;
    }
}
